 import { createOpenAPI, createWebsocket } from './bot';


import {
  AvailableIntentsEventsEnum,
  GetWsParam,
  IntentEvents,
  OpCode,
  SessionEvents,
  SessionRecord,
  WebsocketCloseReason,
  WsAddressObj,
  WsEventType,
  wsResData,
} from '@src/types/websocket-types';


import { IChannel,ChannelType } from '@src/types/index';

import { private_bot_config,public_bot_config } from '@src/config/bot-config';
import { IGuild } from './types';
import { getNews } from './fun/news';

const client = createOpenAPI(private_bot_config);

const ws = createWebsocket(private_bot_config);

let guilds: IGuild[] = []

let channels: IChannel[] = []


ws.on('READY', async(wsdata: wsResData) => {
  console.log('[READY] 事件接收 :', wsdata);


  const response: any = await getNews()
  
  
  //获取频道列表
  const { data } = await  client.meApi.meGuilds()
  guilds = data
  // 获取子频道列表
  if (guilds.length > 0) {
    const { data: datachannels } = await client.channelApi.channels(guilds[0].id)
    channels = datachannels
    console.log(channels)
    //在子频道-论坛子频道-发布帖子
    let channelID= channels.filter(item => item.type == 10007)[0].id
    const content = response.data.news.map((item:any) => {
      return item +'\n' 
    }).join('')
    let news = {
      title: response.data.date,
      content,
      format: 1
    }
    const { data: res } = await client.channelApi.createThreads(channelID, {
      title: response.data.date,
      content,
      format: 1
     // content: "<html lang=\"en-US\"><body><img src=\"https://fc1tn.baidu.com/it/u=1805636791,639113867&fm=202&mola=new&crop=v1\"  /></body></html>",
    })
  }

})
  

ws.on('ERROR', (data: wsResData) => {
  console.log('[ERROR] 事件接收 :', data);
});
ws.on('GUILDS', (data: wsResData) => {
  console.log('[GUILDS] 事件接收 :', data);
});
ws.on('GUILD_MEMBERS', (data: wsResData) => {
  console.log('[GUILD_MEMBERS] 事件接收 :', data);
});
ws.on('GUILD_MESSAGES', (data: wsResData) => {
  console.log('[GUILD_MESSAGES] 事件接收 :', data);
});
ws.on('GUILD_MESSAGE_REACTIONS', (data: wsResData) => {
  console.log('[GUILD_MESSAGE_REACTIONS] 事件接收 :', data);
});
ws.on('DIRECT_MESSAGE', (data: wsResData) => {
  console.log('[DIRECT_MESSAGE] 事件接收 :', data);
});
ws.on('INTERACTION', (data: wsResData) => {
  console.log('[INTERACTION] 事件接收 :', data);
});
ws.on('MESSAGE_AUDIT', (data: wsResData) => {
  console.log('[MESSAGE_AUDIT] 事件接收 :', data);
});
ws.on('FORUMS_EVENT', (data: wsResData) => {
  console.log('[FORUMS_EVENT] 事件接收 :', data);
});
ws.on('AUDIO_ACTION', (data: wsResData) => {
  console.log('[AUDIO_ACTION] 事件接收 :', data);
});
ws.on('PUBLIC_GUILD_MESSAGES', async (eventData: wsResData) => {
  console.log('[PUBLIC_GUILD_MESSAGES] 事件接收 :', eventData);

  const {data} = await client.messageApi.postMessage('', {
    content: 'test'
  })
  console.log(data);
});
ws.on('INTERACTION', (data: wsResData) => {
  console.log('[INTERACTION] 事件接收 :', data);
});



// 获取当前加入了哪些频道






// client.guildApi.guild('').then((data) => {
//   console.log(data);
// });

// // ✅
// client.channelApi.channels(guildID).then((res) => {
//   console.log(res.data);
// });